package com.gusha.service;


import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysTicketDao;
import com.gusha.model.SysClassify;
import com.gusha.model.SysRole;
import com.gusha.model.SysTicket;
import com.gusha.model.SysTicketType;
import com.gusha.model.User;

@Service
@Transactional
public class SysTicketService {
	
@Autowired
private SysTicketDao sysTicketDao;

public Page<SysTicket> findAll(Pageable pageable, SysTicket sysTicket) {
    @SuppressWarnings("serial")
    Page<SysTicket> result = sysTicketDao.findAll(new Specification<SysTicket>() {
        @Override
        public Predicate toPredicate(Root<SysTicket> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> list = new ArrayList<Predicate>();
            if (sysTicket.getId() != null) {
                list.add(cb.equal(root.get("id").as(BigInteger.class),sysTicket.getId()));
            }
            if (sysTicket.getTicketNo() != null) {
                list.add(cb.equal(root.get("ticketNo").as(String.class),sysTicket.getTicketNo()));
            }
            if (sysTicket.getUserId()!= null) {
                list.add(cb.equal(root.get("userId").as(User.class),sysTicket.getUserId()));
            }
            if (sysTicket.getAddTime()!= null) {
                list.add(cb.equal(root.get("addTime").as(Date.class),sysTicket.getAddTime()));
            }
            if (sysTicket.getBuyTime()!= null) {
                list.add(cb.equal(root.get("buyTime").as(Date.class),sysTicket.getBuyTime()));
            }
            if (sysTicket.getUseTime()!= null) {
                list.add(cb.equal(root.get("useTime").as(Date.class),sysTicket.getUseTime()));
            }
            if (sysTicket.getTicketState()!= null) {
                list.add(cb.equal(root.get("ticketState").as(BigInteger.class),sysTicket.getUserId()));
            }
            if (sysTicket.getAdminName()!= null) {
                list.add(cb.equal(root.get("adminName").as(String.class),sysTicket.getAdminName()));
            }
            if (sysTicket.getTicketType()!= null) {
                list.add(cb.equal(root.get("ticketType").as(SysTicketType.class),sysTicket.getTicketType()));
            }
            if (sysTicket.getUserGrade()!= null) {
                list.add(cb.equal(root.get("userGrade").as(BigInteger.class),sysTicket.getUserGrade()));
            }
            if (sysTicket.getDiscount()!= null) {
                list.add(cb.equal(root.get("discount").as(BigDecimal.class),sysTicket.getDiscount()));
            }
            if (sysTicket.getEffectiveTime()!= null) {
                list.add(cb.equal(root.get("effective").as(Date.class),sysTicket.getEffectiveTime()));
            }
            if (sysTicket.getExpirationDate()!= null) {
                list.add(cb.equal(root.get("expirationDate").as(String.class),sysTicket.getExpirationDate()));
            }
            if (sysTicket.getPrice()!= null) {
                list.add(cb.equal(root.get("price").as(String.class),sysTicket.getPrice()));
            }
            
            
            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        }
    },pageable);

	return result;

}

}
